package org.linlinjava.litemall.db.service;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCdkeyMapper;
import org.linlinjava.litemall.db.domain.LitemallCdkey;
import org.linlinjava.litemall.db.domain.LitemallCdkeyExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class LitemallCdkeyService {
    @Resource
    private LitemallCdkeyMapper cdkeyMapper;

    private final static int EXPIRED_YEARS = 2;

    public List<LitemallCdkey> queryIndex() {
        LitemallCdkeyExample example = new LitemallCdkeyExample();
        example.or().andDeletedEqualTo(false);
        return cdkeyMapper.selectByExample(example);
    }

    public List<LitemallCdkey> querySelective(String companyName, String status, Integer page, Integer limit, String sort, String order) {
        LitemallCdkeyExample example = new LitemallCdkeyExample();
        LitemallCdkeyExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(companyName)) {
            criteria.andCompanyNameLike(companyName);
        }
        criteria.andDeletedEqualTo(false);
        if(!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(status.equals("1"));
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        if(page != null && limit != null) {
            PageHelper.startPage(page, limit);
        }
        return cdkeyMapper.selectByExample(example);
    }

    public List<LitemallCdkey> queryByCompanyIdAndChargeId(Integer companyId, Integer chargeId){
        LitemallCdkeyExample example = new LitemallCdkeyExample();
        LitemallCdkeyExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(companyId).andChargeIdEqualTo(chargeId).andDeletedEqualTo(false);
        return cdkeyMapper.selectByExample(example);
    }

    public List<LitemallCdkey> queryByUserId(Integer userId){
        LitemallCdkeyExample example = new LitemallCdkeyExample();
        LitemallCdkeyExample.Criteria criteria = example.createCriteria();
        criteria.andMemberIdEqualTo(userId).andDeletedEqualTo(false);
        return cdkeyMapper.selectByExample(example);
    }

    public List<LitemallCdkey> queryByStatus(boolean status){
        LitemallCdkeyExample example = new LitemallCdkeyExample();
        LitemallCdkeyExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(status).andDeletedEqualTo(false);
        return cdkeyMapper.selectByExample(example);
    }
    public LitemallCdkey queryByCode(String code) {
        LitemallCdkeyExample example = new LitemallCdkeyExample();
        LitemallCdkeyExample.Criteria criteria = example.createCriteria();

        criteria.andCodeEqualTo(code).andDeletedEqualTo(false);
        List<LitemallCdkey> cdkeys = cdkeyMapper.selectByExample(example);
        if(cdkeys.size() > 0){
            return cdkeys.get(0);
        }else{
            return null;
        }
    }

    public int updateById(LitemallCdkey cdkey) {
        cdkey.setUpdateTime(LocalDateTime.now());
        return cdkeyMapper.updateByPrimaryKeySelective(cdkey);
    }

    public void deleteById(Integer id) {
        cdkeyMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallCdkey cdkey) {
        cdkey.setAddTime(LocalDateTime.now());
        cdkey.setUpdateTime(LocalDateTime.now());
        cdkeyMapper.insertSelective(cdkey);
    }

    public LitemallCdkey findById(Integer id) {
        return cdkeyMapper.selectByPrimaryKey(id);
    }

    public Date getExpiredTime(LitemallCdkey cdkey){
        LocalDateTime localDateTime = cdkey.getAddTime();
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);

        Date date = Date.from(zdt.toInstant());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, EXPIRED_YEARS);
        return calendar.getTime();
    }
}
